﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CrusadeGmTool.Models;
using CrusadeGmTool.Controls;
using CrusadeGmTool.Net;

namespace CrusadeGmTool.Forms
{
    public partial class mainForm : Form
    {
        Server _server;

        public mainForm()
        {
            _server = new Server();

            InitializeComponent();
            this.MenuDeconnecter.Enabled = false;
            //met les tab en gris au lieu de blanc moche.
            foreach (Control tab in tabControl1.TabPages)
            {
                tab.BackColor = SystemColors.Control;
            }
            tabControl1.Visible = false;
            _server = new Server();
            _server.connected += (sender, connected) => this.BeginInvoke((MethodInvoker)delegate 
                { 
                    tabControl1.Visible = connected; 
                    this.MenuDeconnecter.Enabled = connected;
                    if (connected) this.label_connectedTo.Text = "Connected to : "+ _server.Adress; 
                });
            _server.UpdatedServerInfo += (sender, arg) => this.BeginInvoke((MethodInvoker)delegate { serverInfoControl1.Serverinfo = _server.ServerInfo; });
        }

    /// <summary>
    /// Affiche la fenetre de connexion rapide
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
        private void seConnecterToolStripMenuItem_Click(object sender, EventArgs e)
        {
            QuickServerChoice choice = new QuickServerChoice();

            if (choice.ShowDialog() == DialogResult.OK)
            {
                if (_server.Connect(choice.loginInfos) == false)
                {
                    MessageBox.Show(String.Format("Impossible de se connecter au server {0}:{1}", _server.Adress, _server.Port));
                }
                
            }
        }

        private void deconnecterToolStripMenuItem_Click(object sender, EventArgs e)
        {
            _server.Disconnect();
        }

        private void mainForm_Resize(object sender, EventArgs e)
        {
            this.tabControl1.Height = this.Height - this.menuStrip1.Height - this.statusStrip1.Height;
            this.tabControl1.Width = this.Width;
        }

        private void favorisToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ServerList favourites = new ServerList();

            if (favourites.ShowDialog() == DialogResult.OK)
            {

            }
        }
    }
}
